package com.vakitdaraliyor.www.dao;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import com.vakitdaraliyor.www.entity.Category;

@Stateless
public class CategoryDao {

	@PersistenceContext
	private EntityManager entityManager;
	
	public CategoryDao() {
		super();
	}
	
	@SuppressWarnings("unchecked")
	public List<Category> hepsiniGetir() {
		return entityManager.createQuery("SELECT i from Category i")
				.getResultList();
	}

	public void ekle(Category category) {
		entityManager.persist(category);
	}

	public void sil(Category category) {
		if (entityManager.contains(category)) {
			entityManager.remove(category);
		} else {
			Category newCategory = (Category) entityManager
					.merge(category);
			entityManager.remove(newCategory);
		}
	}

	public Category gerisayimGuncelle(Category category) {
		entityManager.merge(category);
		return category;
	}
	
	public Category kategoriGetirByName(String name) {
		return (Category) entityManager
				.createQuery("select a from Category a where a.categoryName =:name")
				.setParameter("name", name).getSingleResult();
	}
}
